﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Windows;
using BrainTechLLC.WPF;
using BrainTechLLC;
using ImageAppViewModels;

namespace SettingsWindowsHandler.Plugin
{
    public class SettingsWindowHandler : WindowHandlerBase<SettingsWindow, Window>, ISettingsWindowHandler 
    {
        public event EventHandler RequestClose;
        public static PluginDefinitionLoaded<ISettingsWindowHandler> Handler;
        public ISettingsWatcher SettingsWatcher { get; set; }

        public SettingsWindowHandler(Window parentWindow, PluginDefinitionLoaded<ISettingsWindowHandler> handler)
            : base(parentWindow)
        {
            Handler = handler;
            _parentWindow = parentWindow;
        }

        public override void OpenWindow(Window owner)
        {
            if (_window == null)
            {
                _window = new SettingsWindow() { Owner = owner };
                _window.SettingsViewModel = SettingsWatcher.SettingsViewModel;
                _window.Closed += new EventHandler(_window_Closed);
            }

            ISettingsWindowHandler handler = Handler.Handler;
            handler.RequestClose += new EventHandler(handler_RequestClose);

            _window.Show();
            _window.Focus();
        }

        void handler_RequestClose(object sender, EventArgs e)
        {
            ShutdownWindow();
        }

        private void _window_WantCalcBlurs(object sender, EventArgs e)
        {
            // TODO: HERHEERE
            //Window1.MainWindow.CollectionView.BlurHandler.CalculateBlurs();
        }

        private void _window_WantSaveSettings(object sender, EventArgs e)
        {
            SettingsWatcher.SettingsViewModel.SaveSettings(FilePaths._settingsPath);
        }

        private void _window_Closed(object sender, EventArgs e)
        {
            ShutdownWindow();
        }

        private void ShutdownWindow()
        {
            if (_window != null)
            {
                _window.Closed -= _window_Closed;
                _window.SettingsViewModel = null;
                _window = null;
                RequestClose.RaiseEvent(this);
            }
        }
    }
}
